package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysOperator;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.bo.SysOperatorBo;
import com.xmtlxx.system.domain.vo.SysOperatorVo;
import com.xmtlxx.system.mapper.SysOperatorMapper;
import com.xmtlxx.system.service.ISysOperatorService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 节点经办人Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysOperatorServiceImpl implements ISysOperatorService {

    private final SysOperatorMapper baseMapper;

    /**
     * 查询节点经办人
     *
     * @param opid 节点经办人主键
     * @return 节点经办人
     */
    @Override
    public SysOperatorVo queryById(String opid){
        return baseMapper.selectVoById(opid);
    }

    /**
     * 查询节点经办人列表
     *
     * @param bo 节点经办人
     * @return 节点经办人
     */
    @Override
    public TableDataInfo<SysOperatorVo> queryPageList(SysOperatorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOperator> lqw = buildQueryWrapper(bo);
        Page<SysOperatorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询节点经办人列表
     *
     * @param bo 节点经办人
     * @return 节点经办人
     */
    @Override
    public List<SysOperatorVo> queryList(SysOperatorBo bo) {
        LambdaQueryWrapper<SysOperator> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOperator> buildQueryWrapper(SysOperatorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOperator> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNid()), SysOperator::getNid, bo.getNid());
        lqw.eq(bo.getObjid() != null, SysOperator::getObjid, bo.getObjid());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysOperator::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysOperator::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增节点经办人
     *
     * @param bo 节点经办人
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysOperatorBo bo) {
        SysOperator add = BeanUtil.toBean(bo, SysOperator.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setOpid(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOpid(add.getOpid());
        }
        return flag;
    }

    /**
     * 修改节点经办人
     *
     * @param bo 节点经办人
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysOperatorBo bo) {
        SysOperator update = BeanUtil.toBean(bo, SysOperator.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysOperator entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除节点经办人
     *
     * @param ids 需要删除的节点经办人主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
